package com.tdkj.action.front.member.informationManagement;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoModule;
import com.tdkj.model.information.BeanModuleColumn;
import com.tdkj.model.user.BeanAdminLog;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.information.IBeanBaseInfoService;
import com.tdkj.service.information.IBeanInfoModuleService;
import com.tdkj.util.JsonUtil;

public class InfoCenterAction extends BaseAction {
	private Long id;
	private String classTypeJSON;
	private String mn;
	private String mt;
	private String cn;
	private String ct;
	private String title;
	private String varietyid;
	private String variety;
	private String resource;
	private String content;
	private String[] ids;
	private Map<String, String> prop = new HashMap<String, String>();
	private BeanBaseInfo info;
	private BeanInfoModule infoModule;
	private List<BeanInfoModule> infoModules = new ArrayList<BeanInfoModule>();
	private List<BeanModuleColumn> columns = new ArrayList<BeanModuleColumn>();
	private IBeanInfoModuleService infoModuleService;
	private IBeanBaseInfoService baseInfoService;

	// 查询
	private String infoTitle;
	private String infoContent;
	private String infoState;

	public String queryInfo() {
		// if (StringUtils.isNotEmpty(infoTitle))
		this.getActionContext().getSession().put("infoTitle", infoTitle);

		// if (StringUtils.isNotEmpty(infoContent))
		this.getActionContext().getSession().put("infoContent", infoContent);

		// if (StringUtils.isNotEmpty(infoState))
		this.getActionContext().getSession().put("infoState", infoState);

		return findList();

	}

	private void addCriteria(Criteria criteria, String mt2) {
		criteria.add(Restrictions.like("user", this.getCurrentUser()));

		if (null == this.getActionContext())
			return;

		if (null == this.getActionContext().getSession())
			return;
		if (null != this.getActionContext().getSession().get("infoTag")) {
			if (mt2.equals(this.getActionContext().getSession().get("infoTag"))) {
				if (null != this.getActionContext().getSession().get("infoTitle")) {
					if (StringUtils.isNotEmpty((String) this.getActionContext().getSession().get("infoTitle"))) {
						String tag = (String) this.getActionContext().getSession().get("infoTitle");
						criteria.add(Restrictions.like("title", "%" + tag + "%"));
					}
					this.setInfoTitle((String) this.getActionContext().getSession().get("infoTitle"));
				}

				if (null != this.getActionContext().getSession().get("infoContent")) {
					if (StringUtils.isNotEmpty((String) this.getActionContext().getSession().get("infoContent"))) {
						String tag = (String) this.getActionContext().getSession().get("infoContent");
						criteria.add(Restrictions.like("content", "%" + tag + "%"));

					}
					this.setInfoContent((String) this.getActionContext().getSession().get("infoContent"));
				}

				if (null != this.getActionContext().getSession().get("infoState")) {
					String tag = (String) this.getActionContext().getSession().get("infoState");
					if ("1".equals(tag)) {
						tag = "已审核";
						criteria.add(Restrictions.like("status", tag));
					} else if ("2".equals(tag)) {
						tag = "未审核";
						criteria.add(Restrictions.like("status", tag));
					}

					this.setInfoState((String) this.getActionContext().getSession().get("infoState"));
				}

			} else {
				this.getActionContext().getSession().put("infoTag", mt2);
				this.setInfoTitle("");
				this.setInfoContent("");
				this.setInfoState("");
			}
		} else {
			this.getActionContext().getSession().put("infoTag", mt2);
			this.setInfoTitle("");
			this.setInfoContent("");
			this.setInfoState("");
		}

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getClassTypeJSON() {
		return classTypeJSON;
	}

	public void setClassTypeJSON(String classTypeJSON) {
		this.classTypeJSON = classTypeJSON;
	}

	public String getMn() {
		return mn;
	}

	public void setMn(String mn) {
		this.mn = mn;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getMt() {
		return mt;
	}

	public void setMt(String mt) {
		this.mt = mt;
	}

	public String getVarietyid() {
		return varietyid;
	}

	public void setVarietyid(String varietyid) {
		this.varietyid = varietyid;
	}

	public String getVariety() {
		return variety;
	}

	public void setVariety(String variety) {
		this.variety = variety;
	}

	public String getCn() {
		return cn;
	}

	public void setCn(String cn) {
		this.cn = cn;
	}

	public String getCt() {
		return ct;
	}

	public void setCt(String ct) {
		this.ct = ct;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Map<String, String> getProp() {
		return prop;
	}

	public void setProp(Map<String, String> prop) {
		this.prop = prop;
	}

	public BeanBaseInfo getInfo() {
		return info;
	}

	public void setInfo(BeanBaseInfo info) {
		this.info = info;
	}

	public BeanInfoModule getInfoModule() {
		return infoModule;
	}

	public void setInfoModule(BeanInfoModule infoModule) {
		this.infoModule = infoModule;
	}

	public List<BeanInfoModule> getInfoModules() {
		return infoModules;
	}

	public void setInfoModules(List<BeanInfoModule> infoModules) {
		this.infoModules = infoModules;
	}

	public List<BeanModuleColumn> getColumns() {
		return columns;
	}

	public void setColumns(List<BeanModuleColumn> columns) {
		this.columns = columns;
	}

	public IBeanInfoModuleService getInfoModuleService() {
		return infoModuleService;
	}

	public void setInfoModuleService(IBeanInfoModuleService infoModuleService) {
		this.infoModuleService = infoModuleService;
	}

	@Resource
	public void setBaseInfoService(IBeanBaseInfoService baseInfoService) {
		this.baseInfoService = baseInfoService;
	}

	// 点击返回时使用
	public String findList() {
		mt = this.getMt();
		if (mt.equals("1"))
			return findTrade();
		if (mt.equals("2"))
			return findInfoCenter();
		if (mt.equals("3"))
			return findQuotation();
		if (mt.equals("4"))
			return findMerchants();// 招商引资
		if (mt.equals("5"))
			return findInvest();
		if (mt.equals("6"))
			return findMine();
		if (mt.equals("7"))
			return findMiningright();
		if (mt.equals("8"))
			return findProspect();
		if (mt.equals("9"))
			return findRecruitment();
		if (mt.equals("10"))
			return findHarbor();
		if (mt.equals("11"))
			return findExpert();
		if (mt.equals("12"))
			return findPolicy();
		if (mt.equals("13"))
			return findMineknowledge();
		if (mt.equals("14"))
			return findQuotationnew();
		return "list";
	}

	private String findList(String mt) {
		if (mt.equals("1"))
			return findTrade();
		if (mt.equals("2"))
			return findInfoCenter();
		if (mt.equals("3"))
			return findQuotation();
		if (mt.equals("4"))
			return findMerchants();// 招商引资
		if (mt.equals("5"))
			return findInvest();
		if (mt.equals("6"))
			return findMine();
		if (mt.equals("7"))
			return findMiningright();
		if (mt.equals("8"))
			return findProspect();
		if (mt.equals("9"))
			return findRecruitment();
		if (mt.equals("10"))
			return findHarbor();
		if (mt.equals("11"))
			return findExpert();
		if (mt.equals("12"))
			return findPolicy();
		if (mt.equals("13"))
			return findMineknowledge();
		if (mt.equals("14"))
			return findQuotationnew();
		return "list";
	}

	public String findMineknowledge() {
		// TODO Auto-generated method stub
		mt = "13";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "13")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findMineknowledge.action", 10);
		return "list";
	}

	public String findQuotationnew() {
		// TODO Auto-generated method stub
		mt = "14";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "14")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findQuotationnew.action", 10);
		return "list";
	}

	public String findPolicy() {
		// TODO Auto-generated method stub
		mt = "12";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "12")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findPolicy.action", 10);
		return "list";
	}

	public String findExpert() {
		// TODO Auto-generated method stub
		mt = "11";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "11")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findExpert.action", 10);
		return "list";
	}

	public String findHarbor() {
		// TODO Auto-generated method stub
		mt = "10";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "10")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findHarbor.action", 10);
		return "list";
	}

	public String findRecruitment() {
		// TODO Auto-generated method stub
		mt = "9";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "9")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findRecruitment.action", 10);
		return "list";
	}

	public String findProspect() {
		// TODO Auto-generated method stub
		mt = "8";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "8")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findProspect.action", 10);
		return "list";
	}

	public String findMiningright() {
		// TODO Auto-generated method stub
		mt = "7";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "7")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findMiningright.action", 10);
		return "list";
	}

	public String findMine() {
		// TODO Auto-generated method stub
		mt = "6";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "6")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findMine.action", 10);
		return "list";
	}

	public String findInvest() {
		// TODO Auto-generated method stub
		mt = "5";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "5")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findInvest.action", 10);
		return "list";
	}

	public String findMerchants() {
		// TODO Auto-generated method stub
		mt = "4";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "4")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findMerchants.action", 10);
		return "list";
	}

	public String findQuotation() {
		// TODO Auto-generated method stub
		mt = "3";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "3")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findQuotation.action", 10);
		return "list";
	}

	public String findInfoCenter() {
		// TODO Auto-generated method stub
		mt = "2";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "2")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findInfoCenter.action", 10);
		return "list";
	}

	public String findTrade() {
		// TODO Auto-generated method stub
		mt = "1";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", "1")).addOrder(Order.desc("createtime"));// 对状态进行排序
		addCriteria(criteria, mt);
		this.queryByPage(criteria, "infoCenterAction!findTrade.action", 10);
		return "list";
	}

	public String deleteInfo() {
		baseInfoService.delete(this.getId(), BeanBaseInfo.class);
		return findList(this.getMt());
	}

	public String findAddPage() {
		BeanInfoModule m = new BeanInfoModule();
		m.setModuletype(this.getMt());
		List<BeanInfoModule> ml = infoModuleService.findByExample(m);
		if (ml.size() == 1) {
			infoModule = ml.get(0);
		}
		String location = "";
		if (this.getCt() != null) {
			location = findPageByMode2(this.getMt(), this.getCt());
		} else {
			location = findPageByMode1(this.getMt());
		}
		return location + "_add";
	}

	public String findEditPage() {
		info = baseInfoService.findById(this.getId(), BeanBaseInfo.class);
		BeanInfoModule m = new BeanInfoModule();
		m.setModuletype(this.getMt());
		List<BeanInfoModule> ml = infoModuleService.findByExample(m);
		if (ml.size() == 1) {
			infoModule = ml.get(0);
		}
		String location = findPageByMode1(this.getMt());
		return location + "_edit";
	}

	public String editInfo() {
		id = this.getId();
		mt = this.getMt();
		BeanBaseInfo info = baseInfoService.findById(this.getId(), BeanBaseInfo.class);
		if (this.getTitle() != null)
			info.setTitle(this.getTitle());
		if (this.getContent() != null)
			info.setContent(this.getContent());
		if (this.getMn() != null)
			info.setModulename(this.getMn());
		if (this.getMt() != null)
			info.setModuletype(this.getMt());
		if (this.getCn() != null)
			info.setColumnname(this.getCn());
		if (this.getCt() != null)
			info.setColumntype(this.getCt());
		if (this.getVariety() != null)
			info.setVariety(this.getVariety());
		if (this.getVarietyid() != null)
			info.setVarietyid(this.getVarietyid());
		if (this.getMt() != null)
			mt = this.getMt();
		if (this.getCt() != null)
			ct = this.getCt();
		if (this.getMn() != null)
			mn = this.getMn();
		if (this.getCn() != null)
			cn = this.getCn();
		if (this.getResource() != null)
			info.setResource(this.getResource());
		if (this.getProp() != null) {
			info.setProp(this.getProp());
		}
		BeanUser u = this.getCurrentUser();
		info.setUser(u);
		baseInfoService.save(info);
		return findEditPage();
	}

	// 根据模式一获取地址（moduletype）
	private String findPageByMode1(String mt) {
		String location = "page";
		if (mt.equals("1")) {
			location = "1page";
		}
		if (mt.equals("6")) {
			location = "6page";
		}
		if (mt.equals("7")) {
			location = "7page";
		}
		if (mt.equals("9")) {
			location = "9page";
		}
		return location;
	}

	private String findPageByMode2(String mt, String ct) {
		String location = "page";
		if (mt.equals("3") && (ct.equals("3") || ct.equals("4"))) {
			location = "334page";
		}
		return location;
	}

	// 发布消息
	public String addInfo() {
		BeanBaseInfo info = new BeanBaseInfo();
		if (this.getTitle() == null ||this.getTitle().equals("")) {
			this.addFieldError("titleVlidate", "标题不能为空");
		}
		if (this.getContent() == null ||this.getContent().equals("")) {
			this.addFieldError("contentVlidate", "信息内容不能为空");
		}
		if (this.getTitle() != null)
			info.setTitle(this.getTitle());
		if (this.getContent() != null)
			info.setContent(this.getContent());
		if (this.getMn() != null)
			info.setModulename(this.getMn());
		if (this.getMt() != null)
			info.setModuletype(this.getMt());
		if (this.getCn() != null)
			info.setColumnname(this.getCn());
		if (this.getCt() != null)
			info.setColumntype(this.getCt());
		if (this.getVariety() != null)
			info.setVariety(this.getVariety());
		if (this.getVarietyid() != null)
			info.setVarietyid(this.getVarietyid());
		if (this.getMt() != null)
			mt = this.getMt();
		if (this.getCt() != null)
			ct = this.getCt();
		if (this.getMn() != null)
			mn = this.getMn();
		if (this.getCn() != null)
			cn = this.getCn();
		if (this.getResource() != null)
			info.setResource(this.getResource());
		info.setCreatetime(new Timestamp(System.currentTimeMillis()));
		info.setClicktime(1);
		info.setStatus("未审核");
		if (this.getProp() != null) {
			info.setProp(this.getProp());
		}
		BeanUser u = this.getCurrentUser();
		info.setUser(u);
		baseInfoService.save(info);
		return findAddPage();
	}

	public String deleteAll() {
		String mt = this.getMt();
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanBaseInfo.class);
			}
		}
		return findList(mt);
	}

	/**
	 * @return the infoTitle
	 */
	public String getInfoTitle() {
		return infoTitle;
	}

	/**
	 * @param infoTitle
	 *            the infoTitle to set
	 */
	public void setInfoTitle(String infoTitle) {
		this.infoTitle = infoTitle;
	}

	/**
	 * @return the infoContent
	 */
	public String getInfoContent() {
		return infoContent;
	}

	/**
	 * @param infoContent
	 *            the infoContent to set
	 */
	public void setInfoContent(String infoContent) {
		this.infoContent = infoContent;
	}

	/**
	 * @return the infoState
	 */
	public String getInfoState() {
		return infoState;
	}

	/**
	 * @param infoState
	 *            the infoState to set
	 */
	public void setInfoState(String infoState) {
		this.infoState = infoState;
	}

	/**
	 * @return the baseInfoService
	 */
	public IBeanBaseInfoService getBaseInfoService() {
		return baseInfoService;
	}
}
